package com.xingkeduo.business.operation.domain;

import com.xingkeduo.business.operation.service.DeptmentService;
import com.xingkeduo.business.operation.service.ManagerService;
import com.xingkeduo.business.operation.service.PostsService;
import com.xingkeduo.pojo.dto.Post.PostsDTO;
import com.xingkeduo.pojo.query.ManagerQuery;
import com.xingkeduo.pojo.query.PostsQuery;
import com.xingkeduo.pojo.table.*;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户domain
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class PostsDomain {
    @Autowired
    private DeptmentService deptmentService;
    @Autowired
    private PostsService postsService;

    @Autowired
    private ManagerService managerService;


    public PostsTable getPostsById(Long id) throws Throwable {

        PostsTable postsTable = postsService.selectById(id);

        return postsTable;
    }

    public List<PostsTable> getPostsList(PostsQuery postsQuery) throws Throwable {
        List<PostsTable> postsList = postsService.selectLists(postsQuery);

        return postsList;
    }

    //分页查询
    public Page<PostsDTO> getPostsPageList(PostsQuery postsQuery) throws Throwable {
        Integer pageNo = postsQuery.getPageNo();
        Integer pageSize = postsQuery.getPageSize();
        Page<PostsTable> postsTablePage = postsService.selectPageLists(postsQuery,pageNo,pageSize);

        List<PostsTable> dtoList = postsTablePage.getDtoList();
        ArrayList<PostsDTO> objects = new ArrayList<>();
        if(!CommonUtil.isEmpty(dtoList)){
            for (PostsTable postsTable : dtoList) {
                PostsDTO postsDTO = new PostsDTO();
                BeanUtils.copyProperties(postsTable,postsDTO);
                int managerNum = managerService.count(new ManagerQuery().setPostsId(postsTable.getId()));
                postsDTO.setPostNum(managerNum);
                objects.add(postsDTO);
            }
        }
        Page<PostsDTO> postDTOPage = new Page<>(pageNo,pageSize);
        BeanUtils.copyProperties(postsTablePage, postDTOPage);
        postDTOPage.setDtoList(objects);
        return postDTOPage;
    }

    public int createPosts(PostsTable postsTable) throws Throwable {


        if(!CommonUtil.isEmpty(postsTable.getName())){
            List<PostsTable> postsTableList = postsService.selectLists(new PostsQuery().setNameRepeat(postsTable.getName()));
            if(!CommonUtil.isEmpty(postsTableList)){
                throw new DataException(DataException.Type.DEFAULT, "岗位名字已经存在");
            }
        }

        DeptmentTable deptInfo = new DeptmentTable();
        if(!CommonUtil.isEmpty(postsTable.getDeptId())){
            deptInfo = deptmentService.selectById(postsTable.getDeptId());
        }
        if(!CommonUtil.isEmpty(deptInfo)){
            postsTable.setDeptName(deptInfo.getName());
        }
        return postsService.insert(postsTable);
    }

    public int modifyPosts(PostsTable postsTable) throws Throwable {
        PostsTable postsTable1 = postsService.selectById(postsTable.getId());

        if(!CommonUtil.isEmpty(postsTable.getName())){
            if(!postsTable.getName().equals(postsTable1.getName())){
                if(!CommonUtil.isEmpty(postsTable.getName())){
                    List<PostsTable> postsTableList = postsService.selectLists(new PostsQuery().setNameRepeat(postsTable.getName()));
                    if(!CommonUtil.isEmpty(postsTableList)){
                        throw new DataException(DataException.Type.DEFAULT, "岗位名字已存在");
                    }
                }
            }
        }

        if(!CommonUtil.isEmpty(postsTable.getStatus())){
            if(postsTable.getStatus() == 2){
                List<ManagerTable> managerTableList = managerService.selectLists(new ManagerQuery().setPostsId(postsTable.getId()).setStatus(Integer.valueOf(1)));
                if(!CommonUtil.isEmpty(managerTableList) && managerTableList.size() > 0){
                    throw new DataException(DataException.Type.DEFAULT, "该岗位下，还存在“启用”状态的账号，无法停用岗位!");
                    }
                }else{
                if(!CommonUtil.isEmpty(postsTable1.getDeptId())){
                    DeptmentTable deptmentTable = deptmentService.selectById(postsTable1.getDeptId());
                    if(deptmentTable.getStatus() == 2){
                        throw new DataException(DataException.Type.DEFAULT, "该岗位所对应的的部门状态未启用，请先启用岗位!");
                    }
                }
              }
            }
        int update = postsService.update(postsTable);
        if(!CommonUtil.isEmpty(postsTable.getName())){
            if(update > 0){
                List<ManagerTable> managerTableList = managerService.selectLists(new ManagerQuery().setPostsId(postsTable.getId()));
                for (ManagerTable managerTable : managerTableList) {
                    ManagerTable updateManagerPostName = new ManagerTable();
                    updateManagerPostName.setId(managerTable.getId())
                            .setPostsName(postsTable.getName());
                    managerService.update(updateManagerPostName);
                }
            }
        }

        return update;
    }
    
    //删除
    public int delPosts(Long id) throws Throwable {

        //判断岗位下是否有启用的门店
        if(!CommonUtil.isEmpty(id)){
            List<ManagerTable> managerTableList= managerService.selectLists(new ManagerQuery().setPostsId(id).setStatus(Integer.valueOf(1)));
            if(!CommonUtil.isEmpty(managerTableList) && managerTableList.size() > 0) {
                throw new DataException(DataException.Type.DEFAULT, "该岗位下，还存在“启用”状态的账号，无法删除该岗位!");

            }
        }
        PostsTable postsTable = new PostsTable();

        postsTable.setId(id);
        postsTable.setIsDelete(YesNoEnum.YES);
        int delRes = postsService.delete(postsTable);
        if(delRes > 0){
            return delRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "删除失败");
        }
    }
}
